import Ember from 'ember';
import config from 'game-front/config/environment';

export default Ember.Route.extend({
	moment: Ember.inject.service(),
	share:Ember.inject.service(),
	init() {
		console.log("application.init");
	},
	beforeModel(transition) {
		console.log("application.beforeModel");
		if (window.location.href.indexOf("public=true") > 0) {
			// 跳过无需认证的页面
			return;
		}
		if (config.environment === "production") {
			const userId = sessionStorage.getItem("userId");
			if (!userId) {
				/*跳转到微信登陆*/
				sessionStorage.setItem("returnUrl", window.location.href);
				window.location.replace("/app/index.php?i=4&c=entry&do=game&m=ewei_shop&p=home");
				transition.abort(); // 结束当前请求
			}
		} else {
			sessionStorage.setItem("userId", "oFSaywjSajCATd0Q4NRmbuLoiVx8");
		}

		this.get('moment').changeLocale('zh-cn');
	},
	model() {
		if (window.location.href.indexOf("public=true") > 0) {
			// 跳过无需认证的页面
			return;
		}
		const userId = sessionStorage.getItem("userId");
		if (userId) {
			return Ember.$.getJSON(config.apiBase + "members", {userId:userId, timestamp:new Date().getTime()});
		}
	},
	afterModel: function() {
		console.log("application.afterModel");
		if (window.location.href.indexOf("public=true") > 0) {
			// 跳过无需认证的页面
			return;
		}
		const escapePath = ["allprize", "beforewinners", "start", "gameover"];
		const currentPath = window.location.pathname;
		for (let i = 0; i < escapePath.length; i++) {
			// 跳过不需要的url
			if (currentPath.indexOf(escapePath[i]) > 0) {
				return;
			}
		}
		const share = this.get("share");
		const userId = sessionStorage.getItem("userId");
		const shareLink = {
			title: '90秒夺宝', // 分享标题
			desc: '快来实现一个小目标，参与90秒夺宝赢取大奖',
			link: 'http://game.inkanke.com/yydb/startshare?public=true&key=' + userId, // 分享链接
			imgUrl: 'http://game.inkanke.com/yydb/assets/images/banner-62c08e1120ab2db597edb26e069052c5.png', // 分享图标
			success: function () {
				share.reward(userId);// 赠送奖励
				share.createQR(userId); // 生成推荐二维码
			},
			cancel: function () {
				// 用户取消分享后执行的回调函数
			}
		};
		share.config(shareLink);
	}
});
